<?php

/**
 *  @FileComment            [文件注释内容模板]
 *  @ProjectName             项目名称：ShopFrame
 *  @FileName                   文件名称：ShopCartController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-4 21:50:46
 *  *
 */
class ShopCartController extends CommonController {

        public function index() {
                $this->display();
        }

        public function order() {
                //提交订单页，有内容才提交
                if (session('username') && session('uid')) {
                        if (!empty($_SESSION['cart']['goods'])) {
                                $uid = session('uid');
                                $userInfo = K('user')->get_one_data($uid);
                                $this->assign('userInfo', $userInfo);
                                $this->display();
                        } else {
                                go(U('Index/index'));
                        }
                } else {
                        go(U('Login/index'));
                }
        }

        public function del_cart() {
                //删除购物车的某件商品
                $sid = Q('post.sid');
                $data = array(
                    'sid' => $sid,
                    'num' => 0
                );
                Cart::update($data);
                $this->update_cart();
                $nowData = Cart::getAllData();
                $this->ajax($nowData);
        }

        public function pay() {
                //支付成功界面
                if (session('username') && session('uid')) {
                        //如果有session里面有东西，到支付成功界面，否则跳转至主页
                        if (!empty($_SESSION['cart']['goods'])) {
                                $onumber = Cart::getOrderId();
                                $uid = session('uid');
                                $userInfo = K('user')->get_one_data($uid);
                                $data = array(
                                    'onumber' => $onumber,
                                    'receiver' => $userInfo['nickname'],
                                    'address' => $userInfo['address'],
                                    'totalprice' => Cart::getTotalPrice(),
                                    'time' => time(),
                                    'user_uid' => session('uid')
                                );
                                //添加订单
                                $oid = K('Order')->add_data($data);
                                $cartInfo = Cart::getGoods();
                                $orderList = K('OrderList');
                                $goodsList = K('GoodsList');
                                //添加订单列表和扣除库存
                                foreach ($cartInfo as $v) {
                                        $info = array(
                                            'buynumber' => $v['num'],
                                            'price' => $v['price'] * $v['num'],
                                            'consignee' => $v['option']['stand'],
                                            'order_oid' => $oid,
                                            'goods_gid' => $v['option']['gid'],
                                            'orderstatu' => '待发货',
                                        );
                                        $orderList->add_data($info);
                                        $num = (int) $v['num'];
                                        $glid = (int) $v['id'];
                                        $goodsListInfo = $goodsList->get_one_field_data('glid=' . $glid);
                                        $stock = $goodsListInfo - $num;
                                        $goodsList->update_data($glid, array('stock' => $stock));
                                }
                                //删除session
                                Cart::delAll();
                                //登陆用户session处理方式
                                $this->update_cart();
                                $this->assign('oNumber', $onumber);
                                $this->display();
                        } else {
                                go(U('Index/index'));
                        }
                } else {
                        go(U('Login/index'));
                }
        }

        private function update_cart() {
                //更新购物车的商品数量
                if (session('uid') && session('username')) {
                        $shopCart = K('shopCart');
                        $data = Cart::getAllData();
                        $shopInfo = $shopCart->update_data(session('uid'), $data);
                }
        }

        public function addCart() {
                //添加购物车
                $gid = Q('get.gid', 0, 'intval');
                $glid = Q('get.glid', 0, 'intval');
                $num = Q('get.num', 0, 'intval');
                $goodsInfo = K('Goods')->get_one_data($gid);
                $goodsAttr = K('GoodsList')->get_one_data($glid);
                $goodsDetail = K('GoodsDetail')->get_one_data($gid);
                $goodsDetail['thumb'] = unserialize($goodsDetail['thumb']);
                $thumb = current($goodsDetail['thumb']);
                $comb = $goodsAttr['combattrid'];
                $goodStand = K('GoodsAttr')->get_ganame('gaid in(' . $comb . ')');
                $goodStand = implode(',', $goodStand);
                $data = array(
                    'id' => $glid,
                    'name' => $goodsInfo['gname'],
                    'num' => $num,
                    'price' => $goodsInfo['jdprice'],
                    'option' => array(
                        'thumb' => $thumb['path'],
                        'stand' => $goodStand,
                        'gid' => $gid
                    )
                );
                cart::add($data);
                $this->update_cart();
                $this->header();
                $this->assign('gid', $gid);
                $this->display();
        }

        public function ajax_num() {
                //更新购买数量
                $sid = Q('post.sid');
                $num = Q('post.num', 0, 'intval');
                $data = array(
                    'sid' => $sid,
                    'num' => $num
                );
                Cart::update($data);

                $this->update_cart();
                echo 1; die;
        }

}
